import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../generated/l10n.dart';
import '../../res/colors.dart';
import '../../res/text_style.dart';
import '../../routes/router.dart';
import '../../widget/dialog/dialog_common_style.dart';

class SplashLogic extends GetxController {
  @override
  Future<void> onInit() async {
    super.onInit();
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool isAgree = prefs.getBool("agree_protocol") ?? false;
    if (!isAgree) {
      Get.dialog(CommonDialog(
        title: S.current.appProtocol,
        centerWidget: Column(
          children: [
            RichText(
              textAlign: TextAlign.center,
              text: TextSpan(
                style: TextStyle(
                  fontWeight: FontWeight.w400,
                  fontSize: 16.sp,
                ),
                children: [
                  // 为了保障您的合法权益，请阅读并同意
                  TextSpan(
                    text: S.current.protectRights,
                    style:
                        TextStyles.textStyle(ColorStyle.color_FF1F1F1F, 14.sp),
                  ),
                  TextSpan(
                      text: '《${S.current.userAgreement}》',
                      style: TextStyles.textStyle(
                          ColorStyle.color_ff1877ff, 14.sp),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          Get.toNamed(AppRoutes.web, arguments: {
                            'title': S.current.userAgreement,
                            'url': 'https://www.baidu.com/'
                          });
                        }),
                  TextSpan(
                    text: S.current.and,
                    style:
                        TextStyles.textStyle(ColorStyle.color_FF1F1F1F, 14.sp),
                  ),
                  TextSpan(
                      text: "《${S.current.privacyAgreement}》",
                      style: TextStyles.textStyle(
                          ColorStyle.color_ff1877ff, 14.sp),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          Get.toNamed(AppRoutes.web, arguments: {
                            'title': S.current.privacyAgreement,
                            'url': 'https://www.baidu.com/'
                          });
                        }),
                ],
              ),
            ),
            Container(
              alignment: Alignment.center,
              margin: EdgeInsets.only(top: 13.h),
              child: Text(
                S.current.protectPersonalInfo,
                textAlign: TextAlign.center,
                style: TextStyles.textStyle(ColorStyle.color_FF1F1F1F, 14.sp,
                    fontWeight: FontWeight.w600),
              ),
            ),
          ],
        ),
        posiText: S.current.agree,
        negaText: S.current.disagree,
        posiTap: () {
          prefs.setBool('agree_protocol', true);
          Get.offAllNamed(AppRoutes.mettingList);
        },
        negaTap: () {
          // 退出应用
          exit(0);
        },
      ));
    } else {
      toMain();
    }
  }

  toMain() {
    1000.milliseconds.delay(() => Get.offAllNamed(AppRoutes.mettingList));
  }
}
